﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using StuManModels;
using StuManDAL.Common;

namespace StuManDAL
{
    public class UserService
    {
        /// <summary>
        /// 获得所有用户
        /// </summary>
        /// <returns>用户列表</returns>
        public List<SysUser> GetAllUser()
        {
            string sql = "select UserId,UserName,UserPassword from SysUser";
            SqlDataReader reader = SQLHelper.Instance.GetReader(sql);
            List<SysUser> users = new List<SysUser>();
            while (reader.Read())
            {
                users.Add(new SysUser()
                {
                    UserId = Convert.ToInt32(reader["UserId"]),
                    UserName = reader["UserName"].ToString(),
                    UserPassword = reader["UserPassword"].ToString()
                });
            }
            reader.Close();
            return users;
        }
        /// <summary>
        /// 验证用户
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public SysUser VerifyUser(SysUser user)
        {
            string sql = $"select UserName from SysUser where UserId = {user.UserId} and UserPassword = '{user.UserPassword}'";
            //从数据库中查询
            SqlDataReader reader = SQLHelper.Instance.GetReader(sql);
            if (reader.Read())
            {
                user.UserName = reader["UserName"].ToString();
            }
            else
            {
                user = null;
            }
            reader.Close();
            return user;
        }
        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="user">要添加的用户</param>
        /// <param name="info">执行操作的结果信息</param>
        /// <returns>是否成功</returns>
        public bool AddUser(SysUser user,out string info)
        {
            string sql = $"insert into SysUser(UserName,UserPassword) values('{user.UserName}','{user.UserPassword}')";
            try
            {
                if (SQLHelper.Instance.Update(sql)==1)
                {
                    info = "添加用户成功";
                    return true;
                }
                else
                {
                    info = "添加用户失败";
                    return false;
                }
            }
            catch (Exception ex)
            {
                info = $"添加用户时出现错误，错误信息为{ex.Message}";
                return false;
            }
        }
        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="userId">用户号</param>
        /// <param name="info">执行操作的结果信息</param>
        /// <returns>是否成功</returns>
        public bool DeleteUser(int userId,out string info)
        {
            string sql = $"delete from SysUser where UserId={userId}";
            try
            {
                if (SQLHelper.Instance.Update(sql) == 1)
                {
                    info = "删除用户成功";
                    return true;
                }
                else
                {
                    info = "删除用户失败";
                    return false;
                }
            }
            catch (Exception ex)
            {
                info = $"删除用户时出现错误，错误信息为{ex.Message}";
                return false;
            }
        }
        /// <summary>
        /// 修改用户
        /// </summary>
        /// <param name="newUser">更新用户</param>
        /// <param name="info">执行操作的结果信息</param>
        /// <returns>是否成功</returns>
        public bool ModifyUser(SysUser newUser,out string info)
        {
            string sql = $"update SysUser set UserName='{newUser.UserName}',UserPassword='{newUser.UserPassword}' where UserId='{newUser.UserId}'";
            try
            {
                if (SQLHelper.Instance.Update(sql) == 1)
                {
                    info = "修改用户成功";
                    return true;
                }
                else
                {
                    info = "修改用户失败";
                    return false;
                }
            }
            catch (Exception ex)
            {
                info = $"修改用户时出现错误，错误信息为{ex.Message}";
                return false;
            }

        }
    }
}
